<?php
/**
 * ==============================================
 * Copy right 2015-2016 http://wei.meididi88.com
 * ----------------------------------------------
 * This is not a free software, without any authorization is not allowed to use and spread.
 * ==============================================
 * @author: hy
 * @date: 2015-9-10
 * @version: v1.0.0
 */
namespace Home\Model;

use Think\Model;

/**
 * 评论Model模型类
 */
class CommentsModel extends Model{
	
protected $tableName        =   'comments';
	
// 	protected $autoCheckFields = false;
	
	protected $_validate = array(
		array('post_id','require','评论对象有误'),
		array('url','require', '评论地址'),
		array('uid','require','评论用户'),
		array('to_uid','require', '被评论的用户'),
		array('content','require', '评论内容'),
		array('professional', array(0,1,2,3,4,5),'专业得分值的范围不正确！',2,'in'),
		array('punctuality', array(0,1,2,3,4,5),'守时得分值的范围不正确！',2,'in'),
		array('communication', array(0,1,2,3,4,5),'沟通得分值的范围不正确！',2,'in'),
		array('parentid', 'number','回复目标不能为空！',2),
	);
	
	protected $_auto = array(
		array('avg_score', 'avgScore', 1, 'callback'),
		array('post_table', 'autoComplete', 1, 'callback'),
		array('createtime', 'time', 1, 'function')
	);
	
	/**
	 * 计算平均分
	 * 
	 * @return number
	 */
	public function avgScore() {
		$professional = I ('post.professional');
		$punctuality = I ('punctuality');
		$communication = I ('communication');
		
		if (!$professional && !$punctuality && !$communication) {
			return 0;
		}
		
		$avg_score = round(($professional + $punctuality + $communication)/3, 1);
		return $avg_score;
	}
	
	public function autoComplete() {
		return C('DB_PREFIX').'comments';
	}
}

?>